package com.leadiv.cards.Rook;

import com.leadiv.cards.Deck;
import com.leadiv.cards.Player;
import com.leadiv.cards.Round;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author pborrego
 */
public class RookTable {
    private int nestSize;
    private int playerSize;
    private int dealer;
    private int endScore;

    private Deck<RookCard> deck;
    private Vector<RookCard> nest;
    private Vector<RookPlayer> players;
    private Vector<Integer> scoreCard;
    private Vector<Round> rounds;


    public RookTable()
    {
        // suits, faces and values are standard rook game deck.
        String[] suits = {"Yellow", "Red", "Black", "Green"};
        String[] faces = {"5", "6", "7", "8", "9", "10", "11", "12", "13", "14"};
        Double[] values = {5.0, 0.0, 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 10.0};
        RookCard rook;

        // Default configuration for a standard Rook Game.
        rook = new RookCard("Trump", "Rook", 20);
        this.endScore = 300;
        this.nestSize = 5;
        this.playerSize = 4;
        this.dealer = 0;
        this.nest = new Vector<RookCard>();

        // set up the deck
        this.deck = RookCard.cardFactory(suits, faces, values);
        this.deck.addCard(rook);
        this.deck.shuffle();

        // set up the players
        this.players = new Vector<RookPlayer>();
        for(int i=0; i<this.playerSize; i++)
        {
            this.players.add(new Player());
        }

        this.rounds = new Vector<Round>();

        this.scoreCard = new Vector<Integer>();
        for(int i=0; i<this.playerSize; i++)
            this.scoreCard.add(new Integer(0));
    }

    public void play()
    {
        Round rnd;
        RookPlayer winner;
        int score;

        while(!this.isGameOver())
        {
            rnd = this.startRound();

            rnd.deal();
            winner = rnd.bid();
            rnd.declareTrump(winner);

            while(rnd.hasMoreTricks())
            {
                rnd.playTrick();
            }

            for(int i=0; i<this.playerSize; i++)
            {
                score = rnd.getTeamScore(new Vector<Integer>(Arrays.asList(new Integer(i))));
                this.scoreCard.set(i, new Integer(this.scoreCard.get(i).intValue() + score));
            }

            this.endRound(rnd);
        }

        this.printTable();
    }

    public boolean isGameOver()
    {
        if(this.scoreCard.get(0).intValue() + this.scoreCard.get(2).intValue() >= this.endScore)
            return true;

        if(this.scoreCard.get(1).intValue() + this.scoreCard.get(3).intValue() >= this.endScore)
            return true;

        return false;
    }

    public Round startRound()
    {
        return new Round(this.deck, this.nestSize, this.players, this.dealer);
    }

    public void endRound(Round r)
    {
        this.deck.reset();
        this.dealer = (this.dealer + 1) % this.playerSize;
        this.rounds.add(r);
    }

    public void printTable()
    {
        System.out.println("Current Dealer" + this.dealer);

        Iterator<Round> iter = this.rounds.iterator();
        while(iter.hasNext())
        {
            iter.next().printRound();
        }

        System.out.println("PLAYER'S SCORES");
        Iterator<Integer> iterScore = this.scoreCard.iterator();
        int i = 0;
        while(iterScore.hasNext())
        {
            System.out.println("Player " + i + "'s score " + ": " + iterScore.next().intValue());
            i++;
        }
    }
}